﻿using System;

public class Mapper022 : MapperDefault
{
    public override void init(NES nes)
    {
        base.init(nes);
        this.reset();
    }

    public override void loadROM(ROM rom)
    {
        if (!rom.Valid)
        {
            Console.Out.WriteLine("VRC2: Invalid ROM! Unable to load.");
        }
        else
        {
            int num = rom.RomBankCount * 2;
            this.load8kRomBank(0, 0x8000);
            this.load8kRomBank(1, 0xa000);
            this.load8kRomBank(num - 2, 0xc000);
            this.load8kRomBank(num - 1, 0xe000);
            this.loadCHRROM();
            base.nes.Cpu.requestIrq(2);
        }
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            int num = address;
            if (num <= 0xa000)
            {
                switch (num)
                {
                    case 0x8000:
                        this.load8kRomBank(value_Renamed, 0x8000);
                        return;

                    case 0x9000:
                        value_Renamed = (short) (value_Renamed & 3);
                        if (value_Renamed == 0)
                        {
                            base.nes.Ppu.Mirroring = 0;
                            return;
                        }
                        if (value_Renamed == 1)
                        {
                            base.nes.Ppu.Mirroring = 1;
                            return;
                        }
                        if (value_Renamed == 2)
                        {
                            base.nes.Ppu.Mirroring = 3;
                            return;
                        }
                        base.nes.Ppu.Mirroring = 4;
                        return;
                }
                if (num == 0xa000)
                {
                    this.load8kRomBank(value_Renamed, 0xa000);
                }
            }
            else
            {
                switch (num)
                {
                    case 0xd000:
                        this.load1kVromBank(value_Renamed >> 1, 0x1000);
                        return;

                    case 0xd001:
                        this.load1kVromBank(value_Renamed >> 1, 0x1400);
                        return;

                    case 0xe000:
                        this.load1kVromBank(value_Renamed >> 1, 0x1800);
                        return;

                    case 0xe001:
                        this.load1kVromBank(value_Renamed >> 1, 0x1c00);
                        return;

                    case 0xb000:
                        this.load1kVromBank(value_Renamed >> 1, 0);
                        return;

                    case 0xb001:
                        this.load1kVromBank(value_Renamed >> 1, 0x400);
                        return;

                    case 0xc000:
                        this.load1kVromBank(value_Renamed >> 1, 0x800);
                        return;

                    case 0xc001:
                        this.load1kVromBank(value_Renamed >> 1, 0xc00);
                        return;
                }
            }
        }
    }
}

